/**
 * 测试的过程，最好把测试数据个删除，每个测试数据都调用对应的数据ID去删除
 */

#[cfg(test)]
mod user {
  use reqwest::{blocking, StatusCode};
  use serde_json::{json, Value};

  fn delete_user_by_id(client: &blocking::Client, user: Value) {
    let response = client
      .delete(format!("http://127.0.0.1:8000/todo/{}", user["id"]))
      .send()
      .unwrap();

    assert_eq!(response.status(), StatusCode::NO_CONTENT);
  }

  #[ignore]
  #[test]
  fn it_get_user() {
    let client = reqwest::blocking::Client::new();
    let response = client.get("http://127.0.0.1:8000/user").send().unwrap();

    assert_eq!(response.status(), StatusCode::OK);
  }

  #[ignore]
  #[test]
  fn it_create_user() {
    let client = reqwest::blocking::Client::new();
    let response = client
      .post("http://127.0.0.1:8000/user")
      .json(&json!({
        "todo_id": 3,
        "code": "foo",
        "name": "foo",
        "version": "v1.0.0",
        "description": null,
      }))
      .send()
      .unwrap();

    assert_eq!(response.status(), StatusCode::CREATED);

    let json: Value = response.json().unwrap();
    assert_eq!(
      json,
      json!({
        "id": json["id"],
        "todo_id": 3,
        "code": "foo",
        "name": "foo",
        "version": "v1.0.0",
        "description": json["description"],
        "created_at": json["created_at"]
      })
    );

    let response = client.get("http://127.0.0.1:8000/user").send().unwrap();
    let all_json: Value = response.json().unwrap();
    assert!(all_json.as_array().unwrap().contains(&json));

    delete_user_by_id(&client, json);
  }

  #[ignore]
  #[test]
  fn it_view_user() {
    let client = reqwest::blocking::Client::new();
    let response = client
      .post("http://127.0.0.1:8000/user")
      .json(&json!({
        "todo_id": 4,
        "code": "foo",
        "name": "foo",
        "version": "v1.0.0",
        "description": null,
      }))
      .send()
      .unwrap();

    assert_eq!(response.status(), StatusCode::CREATED);

    let json: Value = response.json().unwrap();
    assert_eq!(
      json,
      json!({
        "id": json["id"],
        "todo_id": 4,
        "code": "foo",
        "name": "foo",
        "version": "v1.0.0",
        "description": json["description"],
        "created_at": json["created_at"]
      })
    );

    let response = client.get("http://127.0.0.1:8000/user").send().unwrap();
    let all_json: Value = response.json().unwrap();
    assert!(all_json.as_array().unwrap().contains(&json));

    delete_user_by_id(&client, json);
  }

  #[ignore]
  #[test]
  fn it_update_user() {
    let client = reqwest::blocking::Client::new();

    let response = client
      .post("http://127.0.0.1:8000/user")
      .json(&json!({
        "todo_id": 4,
        "code": "foo",
        "name": "foo",
        "version": "v1.0.0",
        "description": null,
      }))
      .send()
      .unwrap();

    assert_eq!(response.status(), StatusCode::CREATED);

    let json: Value = response.json().unwrap();
    assert_eq!(
      json,
      json!({
        "id": json["id"],
        "todo_id": 4,
        "code": "foo",
        "name": "foo",
        "version": "v1.0.0",
        "description": json["description"],
        "created_at": json["created_at"]
      })
    );

    let response = client
      .put(format!("http://127.0.0.1:8000/user/{}", json["id"]))
      .json(&json!({
        "todo_id": 7,
        "code": "bar",
        "name": "bar",
        "version": "v2.0.0",
        "description": "xxx",
      }))
      .send()
      .unwrap();

    assert_eq!(response.status(), StatusCode::OK);

    let json: Value = response.json().unwrap();
    assert_eq!(
      json,
      json!({
        "id": json["id"],
        "todo_id": 7,
        "code": "bar",
        "name": "bar",
        "version": "v2.0.0",
        "description": "xxx",
        "created_at": json["created_at"]
      })
    );

    delete_user_by_id(&client, json)
  }

  #[ignore]
  #[test]
  fn it_delete_user() {
    let client = reqwest::blocking::Client::new();

    let response = client
      .post("http://127.0.0.1:8000/todo")
      .json(&json!({
        "email": "abc@qq.com",
        "name": "abc"
      }))
      .send()
      .unwrap();

    assert_eq!(response.status(), StatusCode::CREATED);

    let json: Value = response.json().unwrap();

    delete_user_by_id(&client, json)
  }
}
